package com.cs.beneficios.web.action;

import com.cs.beneficios.business.entity.AtributosDAO;
import com.cs.beneficios.business.entity.BeneficioDAO;
//import com.cs.beneficios.business.entity.RenovarDAO;
import com.cs.beneficios.business.entity.SubProgramaDAO;
import com.cs.beneficios.business.model.Beneficiario_AtributoVO;
import com.cs.beneficios.business.model.SubProgramaVO;
import com.cs.beneficios.common.ConnectionFactory;
import com.cs.paoyer.administration.business.model.UserVO;

import java.io.IOException;

//import java.sql.SQLException;

//import java.text.SimpleDateFormat;

//import java.util.Calendar;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class BeneficioOtorgadoModificadoAction extends Action {
	/**
	 * This is the main action called from the Struts framework.
	 * 
	 * @param mapping
	 *            The ActionMapping used to select this instance.
	 * @param form
	 *            The optional ActionForm bean for this request.
	 * @param request
	 *            The HTTP Request we are processing.
	 * @param response
	 *            The HTTP Response we are processing.
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		// ConnectionFactory cf = new
		// ConnectionFactory("beneficios","beneficios");
		ConnectionFactory cf = new ConnectionFactory();
		HttpSession session = request.getSession();
		// Proceso para la auditoria
		UserVO user = (UserVO) request.getSession().getAttribute("userInfo");
		String terminalAddress = request.getRemoteAddr();
		String strTerminal = ("WEB=" + terminalAddress);
		// Fin del proceso

		List lInsert = new LinkedList();
		String strTercero = "";
		String strCodeAtriDate = null;
		String strPosition = null;
		String codeBot = request.getParameter("codeBot");
		boolean disponibilidad = true;
		boolean existe = false;

		if (!existe) {
			String strBeneficiario = (String) session
					.getAttribute("beneficiario");
			String strCodigoSubpro = (String) session
					.getAttribute("CodigoSubpro");
			String strCodigoSede = (String) session.getAttribute("CodigoSede");
			String strCostBenefit = (String) session
					.getAttribute("CostBenefit");
			String strDateBenefit = (String) session
					.getAttribute("DateBenefit");
			String strTypeBenefit = (String) session
					.getAttribute("TipoBenefit");
			if ((Object) strCodigoSede == null)
				strCodigoSede = request.getParameter("code");
			Object obTercero = (Object) session.getAttribute("listatercero");
			Object obListaInsert = (Object) session
					.getAttribute("listainsetar");

			SubProgramaDAO objSubProgramaDAO;

			if (obTercero == null) {
				lInsert = (List) session.getAttribute("listainsetar");
			} else {
				lInsert = (List) session.getAttribute("listatercero");
				strTercero = (String) session.getAttribute("Tercero");
			}

			if (obListaInsert != null) {
				try {
					BeneficioDAO objBeneDAO = new BeneficioDAO(cf);
					Long loCodigoSede = new Long(strCodigoSede);
					Long loCodigoSubp = new Long(strCodigoSubpro);
					Long loCodigoBenefi = new Long(strBeneficiario);
					objSubProgramaDAO = new SubProgramaDAO(cf);

					SubProgramaVO objsubproVO = new SubProgramaVO();
					objsubproVO = objSubProgramaDAO
							.GetPropiedadesSubpro(loCodigoSubp);
					cf.closeConnection(objSubProgramaDAO.getConn());

					long totaldias = 1;
					// Fin del proceso

					// Proceso para determinar si hay beneficios
					String codeSedeOld = objBeneDAO.getSedeBOT(codeBot);
					if (codeSedeOld.equals(strCodigoSede)) {
						disponibilidad = true;
					} else {
						int disponible = objBeneDAO.GetDisponibilidad(
								strCodigoSede, strCodigoSubpro);
						if (totaldias > disponible) {
							disponibilidad = false;
						}
					}
					// Fin del proceso

					if (disponibilidad) {
						// Proceso para identificar si el beneficio tiene el
						// atributo EDAD
						for (int i = 0; i < lInsert.size(); i++) {
							Beneficiario_AtributoVO objBenefiVO = (Beneficiario_AtributoVO) lInsert
									.get(i);
							AtributosDAO objAtributosDAO = new AtributosDAO(cf);
							String strNameAtri = objAtributosDAO
									.GetNameAtri(objBenefiVO
											.getCodigoatributo());
							cf.closeConnection(objAtributosDAO.getConn());
							if (strNameAtri != null && !strNameAtri.equals("")) {
								if (strNameAtri.equals("EDAD")) {
									strCodeAtriDate = objBenefiVO
											.getCodigoatributo();
									strPosition = String.valueOf(i);
									break;
								}
							}
						}
						// Fin del proceso
						// Proceso para actualizar el beneficio otorgado
						int d = 0;
						String strArrIns[] = { user.getName(), strTerminal };
						/*System.out.println("updatebot: " + strCodigoSede + ", "
								+ loCodigoSubp.longValue() + ", " + lInsert
								+ ", " + strTercero + ", " + strArrIns + ", "
								+ strCostBenefit + ", " + strDateBenefit + ", "
								+ codeBot + ", " + codeSedeOld + ", "
								+ strTypeBenefit);*/
						boolean updated = objBeneDAO.UpdateBOT(strCodigoSede,
								loCodigoSubp, loCodigoBenefi, lInsert,
								strTercero, d, strArrIns, strCostBenefit,
								strDateBenefit, codeBot, codeSedeOld,
								strTypeBenefit);

						if (updated) {
							session.setAttribute("msg", "1");
						}

					} else {
						session.setAttribute("msgdis",
								"No hay beneficios disponibles en la sede seleccionada.");
					}
					cf.closeConnection(objBeneDAO.getConn());
				} catch (Exception e) {
					System.err.println("/******** TDSUB - cwsub");
					e.printStackTrace();
					System.err.println("TDSUB - cwsub ********/");
				}
			}

		} else {
			session.setAttribute("msg", "2");
		}
		return mapping.findForward("success");
	}
}
